angular.module('tab-purchase', ['swiper-x', 'slide-banner', 'img-gallery', 'data-srv'])

.controller 'TabPurchaseCtrl', ($scope, $state, $http, $window, $ionicHistory, $ionicPopover, $ionicPopup, orderPurchase, paginationFactory,userData,showToast, $log,$ionicScrollDelegate) ->
    logger = $log.getInstance 'TabOrdersCtrl'
    logger.info 'load TabOrdersCtrl'
    paginationSrv = new paginationFactory('TabPurchaseCtrl','/tradeapi/')
    paginationSrv.reload(true, {
        action:'mytradelist'
        userid: userData.getToken()
        status: ''
    })
    unWatch = paginationSrv.watch (orderData) ->
        $scope.orders = orderData
    _.extend $scope,
        searchData:
            words:undefined
        doSearch:()->
            paginationSrv.reload(true, {
                action:'mytradelist'
                userid: userData.getToken()
                tradeno:$scope.searchData.words
                status: $scope.orderStatus[orderPurchase.orderStatusIndex].key
            })
        clear: ->
            $scope.searchData.words = undefined
            $scope.doSearch()
        canLoadMore: -> paginationSrv.canLoadMore()
        doRefresh: ->
            paginationSrv.reload(true).finally -> $scope.$broadcast 'scroll.refreshComplete'
        loadMore: ->
            paginationSrv.loadMore().finally -> $scope.$broadcast 'scroll.infiniteScrollComplete'

        goYPK: ->
            $state.go 'tab.market-purchase'
        goodsInClass: (i)->
            orderPurchase.orderStatusIndex = i
            paginationSrv.reload(true, {
                action:'mytradelist'
                userid: userData.getToken()
                status: $scope.orderStatus[i].key
            })
        goSend: (data, event,i)->
            event.stopPropagation()
            console.log 'send'
            $http.post('/orders/', {
                action: 'changestatus',
                userid: userData.getToken(),
                id: data.id
                status: 2
            })
            .success (resp) ->
                if resp?.status == '0'
                    showToast '已发货'
                    $scope.orders.splice(i,1)
                else
                    showToast resp.msg
            .error ->
                showToast '您的网络又调皮了'
        goOrderDetail: (data)->
            orderPurchase.order = data
            $state.go 'tab.purchase-detail'

    $scope.orderStatus = [
        {key: '0', name: '待付款', func: $scope.goodsInClass}
        {key: '1', name: '待发货', func: $scope.goodsInClass}
        {key: '2', name: '待收货', func: $scope.goodsInClass}
        {key: '3', name: '已完成', func: $scope.goodsInClass}
        {key: '', name: '查看全部订单', func: $scope.goodsInClass}
    ]
    $scope.$on '$destory', ->
        logger.log 'TabOrdersCtrl $destory'
        unWatch()
    $scope.$on '$stateChangeSuccess', ->
        if $state.is 'tab.purchase'
            paginationSrv.reload(true, {
                action:'mytradelist'
                userid: userData.getToken()
                status: $scope.orderStatus[orderPurchase.orderStatusIndex].key
            })
            $ionicScrollDelegate.scrollTop()
